<?php

namespace Blog\Controller;

use Blog\Lib\Controller\View;

/**
 * @property \Hx\Http\Request request
 * @property \Blog\Model\Blog blogModel
 * @property \Blog\Model\Comment commentModel Description
 * @property \Blog\Model\Option optionModel
 */
class Blog extends View {

    public function oneAction($id) {
        $this->assignOption();
        $blog = $this->blogModel->getById((int)$id);
        if (!$blog) {
            $this->setView();
            return $this->setError(404);
        }
        $comments = array();
        if ($blog->isComment > 0) {
            $comments = $this->commentModel->getTree($blog->id);
        }
        $this->assignPageTitle($blog->title);
        return $this->view
            ->assign('blog', $blog)
            ->assign('comments', $comments)
            ->setTemplate('blog');
    }

    public function commentAction($id) {
        if (!$this->request->isAjax()) {
            return $this->setError(404);
        }
        $data = $this->input->post(TRUE);
        $data['relateId'] = (int)$id;
        $data['ip'] = $this->request->ip;
        $data['agent'] = $this->request->userAgent;
        $result = $this->commentModel->saveOne($data);
        $this->response->setContentType('application/json;charset=utf-8')
            ->setBody(json_encode($result));
        return $this->response;
    }

}

?>
